//
//  HTMenuFooterView.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/21.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "HTMenuFooterView.h"

@interface HTMenuFooterView()
@property (nonatomic, weak) UIButton *settingBtn;
@property (nonatomic, weak) UIButton *collectBtn;
@property (nonatomic, weak) UILabel *temLabel;
@property (nonatomic, weak) UILabel *cityLabel;

@end

@implementation HTMenuFooterView

+ (instancetype)customFooterView {
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpSubViews];
    }
    
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        [self setUpSubViews];
    }
    
    return self;
}

- (void)setUpSubViews
{
    UIButton *settingBtn = [[UIButton alloc] init];
    settingBtn.titleLabel.font = [UIFont systemFontOfSize:16.5f];
    settingBtn.tag = HTMenuFooterBtnSettingType;
    [settingBtn setTitle:NSLocalizedString(@"设置", nil) forState:UIControlStateNormal];
    [settingBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [settingBtn setImage:[UIImage imageNamed:@"leftMenu_setting_icon"] forState:UIControlStateNormal];
    [settingBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:settingBtn];
    self.settingBtn = settingBtn;
    
    UIButton *collectBtn = [[UIButton alloc] init];
    collectBtn.titleLabel.font = [UIFont systemFontOfSize:16.5f];
    collectBtn.tag = HTMenuFooterBtnCollectType;
    [collectBtn setTitle:NSLocalizedString(@"收藏", nil) forState:UIControlStateNormal];
    [collectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [collectBtn setImage:[UIImage imageNamed:@"leftMenu_ collecting_icon"] forState:UIControlStateNormal];
    [collectBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:collectBtn];
    self.collectBtn = collectBtn;
    
    UILabel *temLabel = [[UILabel alloc] init];
    temLabel.textAlignment = NSTextAlignmentRight;
    temLabel.font = [UIFont systemFontOfSize:22.0f];
    temLabel.textColor = [UIColor blueColor];
    [self addSubview:temLabel];
    self.temLabel = temLabel;
    
    UILabel *cityLabel = [[UILabel alloc] init];
    cityLabel.textAlignment = NSTextAlignmentCenter;
    cityLabel.font = [UIFont systemFontOfSize:16.5f];
    cityLabel.textColor = [UIColor blackColor];
    [self addSubview:cityLabel];
    self.cityLabel = cityLabel;
    
    WeakSelf
    [JSTNewServer requestLocalWeatherInfoSuccess:^(NSDictionary *dataDic) {
        StrongSelf
        self.cityLabel.text = dataDic[@"city"];
        self.temLabel.text = [NSString stringWithFormat:@"%@℃",dataDic[@"data"][@"wendu"]];
        [self.temLabel sizeToFit];
        [self.cityLabel sizeToFit];
    } failure:nil];
}

/**
 *  通过代理传出去
 */
- (void)btnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(menuFooterView:didClickBtnWithType:)]) {
        [self.delegate menuFooterView:self didClickBtnWithType:btn.tag];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 分成3块
    CGFloat itemW = self.frame.size.width / 3;
    CGFloat itemH = self.frame.size.height - 30;

    CGFloat settingBtnX = 10;
    CGFloat settingBtnY = 25;
    CGFloat settingBtnW = itemW - 20;
    CGFloat settingBtnH = itemH - 4;
    self.settingBtn.frame = CGRectMake(settingBtnX, settingBtnY, settingBtnW, settingBtnH);
    
    CGFloat collectBtnX = 10 + itemW;
    CGFloat collectBtnY = settingBtnY;
    CGFloat collectBtnW = settingBtnW;
    CGFloat collectBtnH = settingBtnH;
    self.collectBtn.frame = CGRectMake(collectBtnX, collectBtnY, collectBtnW, collectBtnH);
    
    CGFloat cityLabelX = 2 * itemW;
    CGFloat cityLabelY = settingBtnY;
    CGFloat cityLabelW = itemW;
    CGFloat cityLabelH = settingBtnH;
    self.cityLabel.frame = CGRectMake(cityLabelX, cityLabelY, cityLabelW, cityLabelH);
    
    CGFloat temLabelW = self.temLabel.frame.size.width;
    CGFloat temLabelX = self.frame.size.width - temLabelW - 20;
    CGFloat temLabelY = 0;
    CGFloat temLabelH = self.temLabel.frame.size.height;
    self.temLabel.frame = CGRectMake(temLabelX, temLabelY, temLabelW, temLabelH);
}
@end
